<!-- 耗材资料管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="72px">
      <el-form-item label="耗材编号" prop="goods_id">
        <el-input v-model="queryParams.goods_id" placeholder="请输入耗材编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="耗材名称" prop="goods_name">
        <el-input v-model="queryParams.goods_name" placeholder="请输入耗材名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="goodsList" border>
      <el-table-column label="耗材编号" prop="goods_id" />
      <el-table-column label="耗材名称" prop="goods_name" width="150" show-overflow-tooltip />
      <el-table-column label="耗材规格" prop="goods_spec" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="单位" prop="units" width="60" />
      <el-table-column label="单价" prop="unit_price" />
      <el-table-column label="供应商" prop="supplier" show-overflow-tooltip />
      <el-table-column label="产地" prop="area" show-overflow-tooltip />
      <el-table-column label="中标编码" prop="goods_code" show-overflow-tooltip />
      <el-table-column label="注册证书" prop="cert_number" show-overflow-tooltip />
      <el-table-column label="状态" prop="status_name" width="60" />
      <el-table-column label="操作人" prop="update_user_name" width="120" show-overflow-tooltip />
      <el-table-column label="操作日期" prop="update_date" width="160" />
      <el-table-column label="操作" align="center" fixed="right" width="120" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page_num"
      v-model:limit="queryParams.page_size"
      @pagination="getList"
    />

    <!-- 新增和修改用户信息的弹框 -->
    <GoodsDialog v-model="visible" :rowInfo="rowInfo" @success="success" />
  </div>
</template>

<script setup name="GoodsInfo">
import GoodsDialog from "./goodsDialog.vue";
import { getOptionsList } from "@/api/common";
import { getGoodsList, removeGoods } from "@/api/goods";
const { proxy } = getCurrentInstance();
const showSearch = ref(true);
const loading = ref(false);
const queryParams = ref({
  page_num: 1,
  page_size: 10,
  goods_id: "",
  goods_name: ""
});
const goodsList = ref([]);
const ids = ref([]);
const total = ref(0);
const visible = ref(false);
const rowInfo = ref({});
const statusList = ref([]);

getStatusList();
getList();

async function getStatusList() {
  const res = await getOptionsList({ dict_type: "COMMON_STATUS" });
  if (res.code === 0) {
    statusList.value = res.data;
  }
}

/** 查询列表 */
function getList() {
  loading.value = true;
  const goods_id = queryParams.value.goods_id ? Number(queryParams.value.goods_id) : "";
  getGoodsList({ ...queryParams.value, goods_id })
    .then((res) => {
      const data = res.data;
      goodsList.value = data.list;
      total.value = data.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_num = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 添加修改成功回调
function success(type) {
  if (type === "add") {
    handleQuery();
  } else {
    resetQuery();
  }
}
/** 添加 */
function handleAdd() {
  rowInfo.value = {};
  visible.value = true;
}
/** 修改 */
function handleUpdate(row) {
  rowInfo.value = row;
  visible.value = true;
}
/** 删除按钮操作 */
function handleDelete(row) {
  const name = row.goods_name;
  proxy.$modal
    .confirm('是否确认删除耗材名称为"' + name + '"的数据项?')
    .then(function () {
      return removeGoods({ id: row.goods_id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
</script>
